// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 聊天室
 * 彩种跟投界面（除28）
 */
cc.Class({
    extends: cc.Component,

    properties: {
        rtName:cc.Label,  //玩家名字
        labIssue:cc.RichText,  //期号
        labMoney:cc.RichText,  //钱数
        orderDtailiItem: cc.Prefab,   //内容预制
        swCorll: cc.ScrollView,
        content: cc.Node,
        quickRechargePagePrefab: cc.Prefab,  //支付界面预制
        labSelect: cc.Label,  //彩种类型
        _lotteryid:0, //彩种id
        _data: null,
        _isNorm: null,
    },


    start () {
        if(this._data != null)
        {
            var dataBase = this._data.data;
            this.rtName.string = "玩家:"+dataBase.Data.Name;  
            this.labIssue.string = '<color=#000000>'+"第 "+'</color>'+'<color=#3b85ff>'+this._data.IsuseName+'</color>'+'<color=#000000>'+" 期"+'</color>';
            var numbers = eval('(' + dataBase.Data.LotNumber + ')');//投注内容
            for(var i=0;i<numbers.length;i++)
            {     
                for(var j=0;j<numbers[i].Data.length;j++)
                {
                    var playcode = numbers[i].PlayCode.toString();
                    this._isNorm = numbers[i].Data[0].isNorm;
                   
                    this.labMoney.string = '<color=#000000>'+"金额 " +'</color>'+'<color=#3b85ff>'+this._data.Amount +'</color>'+ '<color=#000000>'+" 元宝"+'</color>';
                    
                    if(dataBase.LotteryName =="幸运28" || dataBase.LotteryName =="加拿大28"){        //彩种为28彩种
                        var content = LotteryUtils.getPlayTypeDecByPlayIdNor(playcode,numbers[i].Data[j].isNorm)+ "  "; 
                        var amo=0;
                        amo= this._data.Amount;
                        content +=  amo.toString()+ "元宝";
                    }else{
                        var content = LotteryUtils.getPlayTypeDecByPlayIdNor(playcode,numbers[i].Data[j].isNorm)+ "  "; 
                        content += numbers[i].Data[j].Bet + "注  ";
                        content += numbers[i].Data[j].Multiple + "倍  ";
                        var amo=0;
                        amo= parseInt(numbers[i].Data[j].Bet) * parseInt(numbers[i].Data[j].Multiple) * 2;
                        content +=  amo.toString()+ "元宝";
                    }
                    var oldNums = "";
                    oldNums = numbers[i].Data[j].Number;
                    if(playcode.length>5)
                    {
                        this._lotteryid = playcode.substring(0,5);
                    }    
                    else
                        this._lotteryid = playcode.substring(0,3);
                    
                    var nums = LotteryUtils.analyseRewardNumber(oldNums,"",playcode,this._lotteryid,false,true);
                    this.content.getChildByName('rtText').getComponent(cc.RichText).string = nums;
                    
                }   
                this.swCorll.scrollToTop(0.1);
            }
            if(!this._isNorm){
                this.labSelect.string = LotteryUtils.getPlayTypeDecByPlayId(playcode);
            }else{
                this.labSelect.string = LotteryUtils.getPlayTypeDecByPlayIdNor(playcode,this._isNorm);
            }
        }
    },

    init: function(data){
        this._data = data;
    
    },

    //确定跟投
    onFollowBet:function(){
        if(!User.getIsvermify())
        {
            ComponentsUtils.showTips("未登录，不能跟投！");
            return;
        }

        if(this._data.BuyType == 1 )
        {
            ComponentsUtils.showTips("追号暂不支持跟单！");
            return;
        }

        var recv = function(ret){
            ComponentsUtils.unblock();
            var obj = ret.Data;
            if(ret.Code === 0 || ret.Code === 90){//预售中可以购买
                if(this._data.IsuseName !== obj.IsuseNum)
                {
                    var log = this._data.IsuseName+"期已截止";
                    ComponentsUtils.showTips(log);
                    this.onClose();
                    return;
                }

                var balace = User.getBalance();
                var toSerMoney = parseFloat(this._data.Amount);
                if(balace<toSerMoney){
                    var quickPayCallback = function(){
                        this.followbet(obj.BeginTime,obj.EndTime);
                    }.bind(this);
                    var quickRechargePage = cc.instantiate(this.quickRechargePagePrefab);
                    var canvas = cc.find("Canvas");
                    canvas.addChild(quickRechargePage);
                    quickRechargePage.getComponent("public_rechargePay_pop").init(this._lotteryid, this._data.IsuseName,toSerMoney,this._data.BuyType, false,quickPayCallback);
                }
                else{
                    this.followbet(obj.BeginTime,obj.EndTime);
                }
            }
            else
            {
                ComponentsUtils.showTips(ret.Msg);
            }
        }.bind(this);
        var data = {
            Token:User.getLoginToken(),
            LotteryCode:this._lotteryid,
        }
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETRECURRENTISUSE, data, recv.bind(this),"POST");   
        ComponentsUtils.block();
    },

    followbet:function(BeginTime,EndTime){
        //POST跟投
        var recv0 = function(ret0){
            ComponentsUtils.unblock();
            if (ret0.Code != 0) {
                ComponentsUtils.showTips(ret0.Msg);
            } else {
                window.Notification.emit("moneyShow",null);
                this.onClose();
            }
        }.bind(this);
            
        var data = {
            UserCode:User.getUserCode(),
            OrderCode:this._data.OrderCode,
            Token:User.getLoginToken(),
            IsuseNum:this._data.IsuseName,
            BeginTime:BeginTime,
            EndTime:EndTime
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.FOLLOWBET, data, recv0.bind(this),"POST");
        ComponentsUtils.block();
    },

    onClose:function(){
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
